import java.util.ArrayList;
import java.util.List;

/**
 * 46. 全排列
 * https://leetcode.cn/problems/permutations/description/
 */
class Solution1 {
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permute(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        int len = nums.length;
        check = new boolean[len];
        dfs(nums);
        return ret;
    }
    public void dfs(int[] nums) {
        int len = nums.length;
        if(path.size() == len) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < len; i++) {
            if(check[i] == false) {
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                check[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }

}